var $ = require('../../../utils/helper.js'),
  that = null
var app = getApp()
Page({
  data: {
    info: {
      type: 1,
    },
    cate_index: 0
  },
  onLoad(e) {
    that = this
  },
  onShow: function() {

  },
  onReady: function() {
    $.check(that)
  },
  init: function() {
    that.catelist()
  },
  changetype: function(e) {
    that.setData({
      ["info.type"]: e.detail.value
    })
  },
  setinput: function(e) {
    that.setData({
      ["info." + e.currentTarget.dataset.name]: e.detail.value
    })
    console.log(that.data)
  },
  upload: function(e) {
    console.log(e)
    wx.chooseImage({
      success(res) {
        let tempFilePaths = res.tempFilePaths
        $.upload("auth", tempFilePaths[0], function(data) {
          let piclist = that.data.info.piclist
          piclist.push(data);
          that.setData({ ['info.piclist']: piclist })
        })
      }
    })
  },
  save: function() {
    $.post("api/signup/edit", that.data.info, function() {
      wx.navigateBack({

      })
    })
  },
  setmap: function() {
    wx.chooseLocation({
      success: function(res) {
        console.log(res)
        that.setData({
          ["info.latitude"]: res.latitude
        })
        that.setData({
          ["info.longitude"]: res.longitude
        })
        that.setData({
          ["info.address"]: res.address
        })
      }
    })
  },
  catelist: function() {
    $.post("api/signup/cate_lists", null, function(lists) {
      that.setData({
        cate_lists: lists
      })

      $.post("api/signup/user_get", null, function (data) {
        if (data != null) {
          that.setData({
            info: data
          })
          let signup_cate_id=data.signup_cate_id
          let cate_lists = that.data.cate_lists
          for (var i in cate_lists){
            if (cate_lists[i].id == signup_cate_id){
              that.setData({cate_index:i})
            }
          }
        }

      })
      that.setcate_core()
    })
  },
  setcate: function(e) {
    that.setData({
      cate_index: e.detail.value
    })
    that.setcate_core()

  },
  setcate_core: function() {
    that.setData({
      ['info.signup_cate_id']: that.data.cate_lists[that.data.cate_index].id
    })
    console.log(that.data.cate_lists)
  },
  remove:function(e){
    console.log(e.currentTarget.dataset.index)
    let index = e.currentTarget.dataset.index;
    wx.showModal({
      title: '操作确认',
      content: '确定要删除该图片么',
      success:function(res){
        console.log(res)
        if (res.confirm){
          let piclist=that.data.info.piclist
        
          piclist.splice(index, 1);
          that.setData({ ['info.piclist']:piclist})
        }
      }
    })

  }
})